package com.whmall.service.crm.module.invoice.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.component.util.FieldUtils;
import com.whmall.component.util.NumberUtils;
import com.whmall.service.crm.entity.dto.invoice.SubstituteInvoiceCompanyAddReqDTO;
import com.whmall.service.crm.entity.dto.invoice.SubstituteInvoiceCompanyQueryReqDTO;
import com.whmall.service.crm.entity.dto.invoice.SubstituteInvoiceCompanyQueryRspDTO;
import com.whmall.service.crm.entity.dto.invoice.SubstituteInvoiceCompanyUpdateReqDTO;
import com.whmall.service.crm.module.company.entity.CompanyDO;
import com.whmall.service.crm.module.invoice.dao.SubstituteInvoiceCompanyMapper;
import com.whmall.service.crm.module.invoice.entity.SettleCollegeAccountDO;
import com.whmall.service.crm.module.invoice.entity.SubstituteInvoiceCompanyDO;
import com.whmall.service.crm.module.invoice.entity.qo.SubstituteInvoiceCompanyQO;
import com.whmall.service.crm.module.invoice.service.SubstituteInvoiceCompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubstituteInvoiceCompanyServiceImpl implements SubstituteInvoiceCompanyService {

    @Autowired
    private SubstituteInvoiceCompanyMapper substituteMapper;

    @Autowired
    private SubstituteInvoiceCompanyValidator substituteCompanyValidator;

    @Override
    public List<SubstituteInvoiceCompanyQueryRspDTO> query(SubstituteInvoiceCompanyQueryReqDTO reqDTO) {
        return substituteMapper.selectBy(
                SubstituteInvoiceCompanyQO.builder()
                        .substituteUseRange(reqDTO.getSubstituteUseRange())
                        .id(reqDTO.getSubstituteInvoiceCompanyId())
                        .useProduct(reqDTO.getUseProduct())
                        .isEnabled(1)
                        .orderBy("id desc").build())
                .stream().map(e -> {
                    SubstituteInvoiceCompanyQueryRspDTO rspDTO = new SubstituteInvoiceCompanyQueryRspDTO();
                    BeanUtils.copyProperties(e, rspDTO);
                    rspDTO.setSubstituteUseRange(FieldUtils.unwrapArray(e.getSubstituteUseRange(), "-", Integer.class));
                    rspDTO.setSubstitutePoint(NumberUtils.divide100(e.getSubstitutePoint()));
                    return rspDTO;
                }).collect(Collectors.toList());
    }

    @Override
    public PageRspDTO<SubstituteInvoiceCompanyQueryRspDTO> queryPage(SubstituteInvoiceCompanyQueryReqDTO reqDTO,
                                                                     PageReqDTO pageReqDTO) {

        SubstituteInvoiceCompanyQO substituteInvoiceCompanyQO = SubstituteInvoiceCompanyQO.builder()
                .substituteUseRange(reqDTO.getSubstituteUseRange())
                .id(reqDTO.getSubstituteInvoiceCompanyId())
                .orderBy("create_time desc").build();
        PageInfo<SubstituteInvoiceCompanyDO> pageInfo = PageHelper.startPage(pageReqDTO.getPageNum(),
                pageReqDTO.getPageSize()).doSelectPageInfo(() -> substituteMapper.selectBy(substituteInvoiceCompanyQO));
        List<SubstituteInvoiceCompanyQueryRspDTO> settleCollegeAccountRspDTOS = new ArrayList<>();
        pageInfo.getList().forEach(e -> {
            SubstituteInvoiceCompanyQueryRspDTO rspDTO = new SubstituteInvoiceCompanyQueryRspDTO();
            BeanUtils.copyProperties(e, rspDTO);
            rspDTO.setSubstituteUseRange(FieldUtils.unwrapArray(e.getSubstituteUseRange(), "-", Integer.class));
            rspDTO.setUseProduct(FieldUtils.unwrapArray(e.getUseProduct(), "-", Integer.class));
            rspDTO.setSubstitutePoint(NumberUtils.divide100(e.getSubstitutePoint()));
            settleCollegeAccountRspDTOS.add(rspDTO);
        });
        return new PageRspDTO<>(pageInfo.getPageNum(), pageInfo.getPageSize(),
                pageInfo.getPages(), (int) pageInfo.getTotal(), settleCollegeAccountRspDTOS);
    }

    @Override
    @Transactional
    public Long create(SubstituteInvoiceCompanyAddReqDTO reqDTO) {

        // 验证, 创建
        substituteCompanyValidator.validate(reqDTO);
        SubstituteInvoiceCompanyDO company = new SubstituteInvoiceCompanyDO();
        BeanUtils.copyProperties(reqDTO, company);

        company.setSubstituteUseRange(FieldUtils.wrapArray(reqDTO.getSubstituteUseRange(), "-", true));
        company.setUseProduct(FieldUtils.wrapArray(reqDTO.getUseProduct(), "-", true));
        //将公司id设为  待开票公司的主键id
        company.setId(reqDTO.getCompanyId());
        //默认启用
        company.setIsEnabled(1);
        company.setSubstitutePoint(NumberUtils.multiply100(reqDTO.getSubstitutePoint()));
        company.setCreateId(reqDTO.getEmployeeId());
        company.setCreateTime(new Date());
        company.setEnableId(reqDTO.getEmployeeId());
        company.setEnableTime(new Date());
        substituteMapper.insertSelective(company);
        return company.getId();
    }

    @Override
    @Transactional
    public void update(SubstituteInvoiceCompanyUpdateReqDTO reqDTO) {

        // 验证, 创建
        substituteCompanyValidator.validateUpdate(reqDTO);
        SubstituteInvoiceCompanyDO company = substituteMapper.selectById(reqDTO.getId());
        BeanUtils.copyProperties(reqDTO, company);

        List<Integer> substituteUseRange = reqDTO.getSubstituteUseRange();
        String useRange = FieldUtils.wrapArray(substituteUseRange, "-", true);
        company.setSubstituteUseRange(useRange);
        company.setUseProduct(FieldUtils.wrapArray(reqDTO.getUseProduct(), "-", true));
        company.setUpdateId(reqDTO.getEmployeeId());
        company.setUpdateTime(new Date());
        company.setSubstitutePoint(NumberUtils.multiply100(reqDTO.getSubstitutePoint()));
        substituteMapper.updateByIdSelective(company);
    }

    @Override
    @Transactional
    public void enable(Long id, Long employeeId) {

        Assert.notNull(id, "代开发票公司不能为空");

        SubstituteInvoiceCompanyDO substituteInvoiceCompanyDO = substituteMapper.selectById(id);
        if (substituteInvoiceCompanyDO == null){
            throw new IllegalArgumentException("数据异常");
        }
        Integer isEnable = substituteInvoiceCompanyDO.getIsEnabled();
        if (isEnable.equals(1)){
            substituteInvoiceCompanyDO.setIsEnabled(0);
        }else {
            substituteInvoiceCompanyDO.setIsEnabled(1);
        }
        substituteInvoiceCompanyDO.setEnableId(employeeId);
        substituteInvoiceCompanyDO.setEnableTime(new Date());
        substituteMapper.updateByIdSelective(substituteInvoiceCompanyDO);
    }

}
